//
//  DataDetailViewController.swift
//  BellManage
//
//  Created by Bestsch on 16/5/10.
//  Copyright © 2016年 wsl. All rights reserved.
//

import UIKit

class DataDetailViewController: BaseViewController {
    
    var segmentedControl: UISegmentedControl!
    
    var teacherLabel,familyLabel,PVLabel,UVLabel, ALLLabel: UILabel!
    
    var teacherCountLabel, familyCountLabel, todayLabel, totleLabel: UILabel!
    var view1,view2,view3,view4,view5,view6: UIView!
    
    var teacher: String!,family: String!,PV: String!,UV: String!
    
    var hud: MBProgressHUD!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        
//        self.title = "数据统计"
        self.navigationItem.setNewTitle("数据统计")
        
        let array = ["今日访问","本周访问","所有访问"]
        segmentedControl = UISegmentedControl.init(items: array)
        segmentedControl.frame = CGRect(x: 30, y: 74, width: screenW - 60, height: 40)
        segmentedControl.selectedSegmentIndex = 0
        segmentedControl.tintColor = barColor
        
        
        segmentedControl.addTarget(self, action: #selector(BellStatisViewController.selTab), for: UIControlEvents.valueChanged)
        self.view.addSubview(segmentedControl)
        
        InitViews()
        
        getStatis("1")
    }
    func InitViews() {
        //访问量 数量
        PVLabel = UILabel.init(frame: CGRect(x: 0 , y: segmentedControl.frame.maxY + 30, width: screenW, height: 80))
        PVLabel.backgroundColor = UIColor.clear
        PVLabel.numberOfLines = 0
        PVLabel.textAlignment = NSTextAlignment.center
        
        //活跃用户 数量
        UVLabel = UILabel.init(frame: CGRect(x: 0, y: PVLabel.frame.maxY, width: screenW, height: 80))
        UVLabel.backgroundColor = UIColor.clear
        UVLabel.numberOfLines = 0
        UVLabel.textAlignment = NSTextAlignment.center
        
//        //总用户数 - segmengt == 3
//        ALLLabel = UILabel.init(frame: CGRect(x: 0, y: UVLabel.frame.maxY, width: screenW, height: 80))
//        ALLLabel.backgroundColor = UIColor.clear
//        ALLLabel.numberOfLines = 0
//        ALLLabel.textAlignment = NSTextAlignment.center
        
        // 今日注册数量
        todayLabel = UILabel.init(frame: CGRect(x: 0, y: UVLabel.frame.maxY + 10, width: screenW, height: 60))
        todayLabel.backgroundColor = UIColor.clear
        todayLabel.textColor = UIColor.darkText
        
        todayLabel.numberOfLines = 0
        todayLabel.textAlignment = NSTextAlignment.center
        
        view1 = UIView.init(frame: CGRect(x: 0, y: todayLabel.frame.maxY + 0, width: screenW, height: 1))
        view1.backgroundColor = UIColor.init(hexString: "eeeeee", alpha: 1)
        
        //注册教师数量 - 今日
        teacherLabel = UILabel.init(frame: CGRect(x: 0, y: todayLabel.frame.maxY + 0, width: screenW/2, height: 60))
        teacherLabel.backgroundColor = UIColor.clear
        teacherLabel.textColor = UIColor.darkText
        teacherLabel.numberOfLines = 0
        teacherLabel.textAlignment = NSTextAlignment.center
        
        view2 = UIView.init(frame: CGRect(x: screenW/2, y: todayLabel.frame.maxY + 10, width: 1, height: 40))
        view2.backgroundColor = UIColor.init(hexString: "eeeeee", alpha: 1)
        
        //注册家长数量 - 今日
        familyLabel = UILabel.init(frame: CGRect(x: screenW/2, y: todayLabel.frame.maxY + 0, width: screenW/2, height: 60))
        familyLabel.backgroundColor = UIColor.clear
        familyLabel.textColor = UIColor.darkText
        familyLabel.numberOfLines = 0
        familyLabel.textAlignment = NSTextAlignment.center
        
        view3 = UIView.init(frame: CGRect(x: 0, y: familyLabel.frame.maxY, width: screenW, height: 1))
        view3.backgroundColor = UIColor.init(hexString: "eeeeee", alpha: 1)
        
        // 今日注册数量
        totleLabel = UILabel.init(frame: CGRect(x: 0, y: familyLabel.frame.maxY + 10, width: screenW, height: 60))
        
        totleLabel.backgroundColor = UIColor.clear
        totleLabel.textColor = UIColor.darkText
        totleLabel.numberOfLines = 0
        totleLabel.textAlignment = NSTextAlignment.center
        
        view4 = UIView.init(frame: CGRect(x: 0, y: totleLabel.frame.maxY, width: screenW, height: 1))
        view4.backgroundColor = UIColor.init(hexString: "eeeeee", alpha: 1)
        
        //注册教师数量 - 总数
        teacherCountLabel = UILabel.init(frame: CGRect(x: 0, y: totleLabel.frame.maxY + 0, width: screenW/2, height: 60))
        teacherCountLabel.backgroundColor = UIColor.clear
        teacherCountLabel.textColor = UIColor.darkText
        teacherCountLabel.numberOfLines = 0
        teacherCountLabel.textAlignment = NSTextAlignment.center
        
        view5 = UIView.init(frame: CGRect(x: screenW/2, y: view4.frame.maxY + 10, width: 1, height: 40))
        view5.backgroundColor = UIColor.init(hexString: "eeeeee", alpha: 1)
        
        //注册家长 数量 - 总数
        familyCountLabel = UILabel.init(frame: CGRect(x: screenW/2, y: totleLabel.frame.maxY + 0, width: screenW/2, height: 60))
        familyCountLabel.backgroundColor = UIColor.clear
        familyCountLabel.textColor = UIColor.darkText
        familyCountLabel.numberOfLines = 0
        familyCountLabel.textAlignment = NSTextAlignment.center
        
        view6 = UIView.init(frame: CGRect(x: 0, y: familyCountLabel.frame.maxY, width: screenW, height: 1))
        view6.backgroundColor = UIColor.init(hexString: "eeeeee", alpha: 1)
        
        
        self.view.addSubview(PVLabel)
        self.view.addSubview(UVLabel)
//        self.view.addSubview(ALLLabel)
        
        self.view.addSubview(todayLabel)
        self.view.addSubview(teacherLabel)
        self.view.addSubview(familyLabel)
        
        self.view.addSubview(totleLabel)
        self.view.addSubview(teacherCountLabel)
        self.view.addSubview(familyCountLabel)
        
        
        self.view.addSubview(view1)
        self.view.addSubview(view2)
        self.view.addSubview(view3)
        self.view.addSubview(view4)
        self.view.addSubview(view5)
        self.view.addSubview(view6)
        

    }
    func selTab(){
        let n = segmentedControl.selectedSegmentIndex
        switch n {
        case 0:
            getStatis("1")
            break
        case 1:
            getStatis("2")
            break
        case 2:
            getStatis("0")
            break
        default:
            break
        }
    }
    /**
     获取统计
     
     - parameter type: 1今日，2为周，0为所有
     */
    func getStatis(_ type: String!) {
        hud = MBProgressHUD.showAdded(to: self.view, animated: true)
        hud.animationType = MBProgressHUDAnimation.fade
        
        let queue = DispatchQueue(label: "queue")
        queue.async {
            let user = getUserPojo()
            let requestXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><rss version=\"2.0\"><channel><t>4</t><TimeType>\(type!)</TimeType><UpperCode>\(user.UpperCode!)</UpperCode></channel></rss>"
            let url = server + superWebapp
            
            HttpPostUTF(url, requestXML: requestXml, finished: { (data, result, error) in
                if error == nil {
                    let array = JsonFormt(str: result) 
                    if array.count != 0 {
                        for i in 0...array.count - 1 {
                            let dic = array[i] as! JSON
                            // 教师注册 - 家长注册
                            let teacherNum = dic["teareg"].stringValue
                            let familyNum = dic["famreg"].stringValue
                            // 总访问数 - 活跃数
                            let PVNum = dic["pvnum"].stringValue
                            let UVNum = dic["uvnum"].stringValue
                            
                            // 总教师 - 总家长 - 总共
                            var allteareg = dic["allteareg"].stringValue
                            var allfamreg = dic["allfamreg"].stringValue
                            let allregnum = dic["allregnum"].stringValue
                            
                            if allteareg == "" {
                                allteareg = "0"
                            }
                            if allfamreg == "" {
                                allfamreg = "0"
                            }
                            
                            let PV = PVNum + "\n访问量"
                            let pv = NSMutableAttributedString.init(string: PV)
                            pv.addAttribute(NSFontAttributeName, value: UIFont.boldSystemFont(ofSize: 45), range: NSMakeRange(0, PVNum.characters.count))
                            pv.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 14), range: NSMakeRange(PVNum.characters.count, pv.length - PVNum.characters.count))
                            pv.addAttribute(NSForegroundColorAttributeName, value: barColor, range: NSMakeRange(0, PVNum.characters.count))
                            pv.addAttribute(NSForegroundColorAttributeName, value: UIColor.darkText, range: NSMakeRange(PVNum.characters.count, pv.length - PVNum.characters.count))
                            self.PVLabel.attributedText = pv
                            
                            let UV = UVNum + "\n活跃用户"
                            let uv = NSMutableAttributedString.init(string: UV)
                            uv.addAttribute(NSFontAttributeName, value: UIFont.boldSystemFont(ofSize: 40), range: NSMakeRange(0, UVNum.characters.count))
                            uv.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 14), range: NSMakeRange(UVNum.characters.count, uv.length - UVNum.characters.count))
                            uv.addAttribute(NSForegroundColorAttributeName, value: barColor, range: NSMakeRange(0, UVNum.characters.count))
                            uv.addAttribute(NSForegroundColorAttributeName, value: UIColor.darkText, range: NSMakeRange(UVNum.characters.count, uv.length - UVNum.characters.count))
                            self.UVLabel.attributedText = uv
                            
//                            let ALL = allregnum + "\n总用户数"
//                            let all = NSMutableAttributedString.init(string: ALL)
//                            all.addAttribute(NSFontAttributeName, value: UIFont.boldSystemFont(ofSize: 40), range: NSMakeRange(0, allregnum.characters.count))
//                            all.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 14), range: NSMakeRange(allregnum.characters.count, all.length - allregnum.characters.count))
//                            all.addAttribute(NSForegroundColorAttributeName, value: barColor, range: NSMakeRange(0, allregnum.characters.count))
//                            all.addAttribute(NSForegroundColorAttributeName, value: UIColor.darkText, range: NSMakeRange(allregnum.characters.count, all.length - allregnum.characters.count))
//                            self.ALLLabel.attributedText = all
                            
                            // 今日、注册数量
                            let Teacher = "教师\n" + teacherNum
                            let teacher = NSMutableAttributedString.init(string: Teacher)
                            teacher.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 14), range: NSMakeRange(0, 2))
                            teacher.addAttribute(NSFontAttributeName, value: UIFont.boldSystemFont(ofSize: 30), range: NSMakeRange(2, teacher.length - 2))
//                            self.teacherLabel.attributedText = teacher
                            
                            let Family = "家长\n" + familyNum
                            let family = NSMutableAttributedString.init(string: Family)
                            family.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 14), range: NSMakeRange(0, 2))
                            family.addAttribute(NSFontAttributeName, value: UIFont.boldSystemFont(ofSize: 30), range: NSMakeRange(2, family.length - 2))
//                            self.familyLabel.attributedText = family
                            
                            
                            // 本周、注册数量
                            let totleTeacher = "教师\n" + allteareg
                            let countTeacher = NSMutableAttributedString.init(string: totleTeacher)
                            countTeacher.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 14), range: NSMakeRange(0, 2))
                            countTeacher.addAttribute(NSFontAttributeName, value: UIFont.boldSystemFont(ofSize: 30), range: NSMakeRange(2, countTeacher.length - 2))
                            
                            self.teacherCountLabel.attributedText = countTeacher
                            
                            let totleFamily = "家长\n" + allfamreg
                            let countFamily = NSMutableAttributedString.init(string: totleFamily)
                            countFamily.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 14), range: NSMakeRange(0, 2))
                            countFamily.addAttribute(NSFontAttributeName, value: UIFont.boldSystemFont(ofSize: 30), range: NSMakeRange(2, countFamily.length - 2))
                            
                            self.familyCountLabel.attributedText = countFamily
                            
                            
                            if self.segmentedControl.selectedSegmentIndex == 0 {
                                self.todayLabel.text = "今日注册数"
                            }else if self.segmentedControl.selectedSegmentIndex == 1 {
                                self.todayLabel.text = "本周注册数"
                            }else if self.segmentedControl.selectedSegmentIndex == 2 {
                                self.todayLabel.text = "总用户数"
                            }
                            
                            
                            self.todayLabel.font = UIFont.boldFlatFont(ofSize: 20.0)
                            
                            
                            self.totleLabel.font = UIFont.boldFlatFont(ofSize: 20.0)
                            
                            if type == "1" || type == "2" {
                                self.teacherLabel.attributedText = teacher
                                self.familyLabel.attributedText = family
                                self.totleLabel.text = "总用户数"
                                self.view4.isHidden = false
                                self.view5.isHidden = false
                                self.view6.isHidden = false
                            }else {

                                self.teacherLabel.attributedText = countTeacher
                                self.familyLabel.attributedText = countFamily
                                
                                self.totleLabel.text = ""
                                self.teacherCountLabel.text = ""
                                self.familyCountLabel.text = ""
                                self.view4.isHidden = true
                                self.view5.isHidden = true
                                self.view6.isHidden = true
                            }
                            
                            
                            
                            
                        }
                    }
                    self.hud.hide(true)
                }else {
                    let alert = UIAlertView.init(title: "错误提示", message: error?.localizedDescription, delegate: nil, cancelButtonTitle: "知道了")
                    alert.show()
                    self.hud.hide(true)
                }
            })
           
            DispatchQueue.main.async(execute: {
                
            })
        }
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
